package com.c2c.service.impl;

import com.c2c.mapper.ComplaintMapper;
import com.c2c.mapper.GoodsMapper;
import com.c2c.mapper.OrdersMapper;
import com.c2c.mapper.UserMapper;
import com.c2c.po.Complaint;
import com.c2c.po.Goods;
import com.c2c.po.Orders;
import com.c2c.po.enums.impl.ComplaintsState;
import com.c2c.service.ComplaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ComplaintServiceImpl implements ComplaintService {

    @Autowired
    ComplaintMapper complaintMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    OrdersMapper ordersMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public List<Complaint> findAllComplaint() {
        return complaintMapper.findAllComplaint();
    }

    @Override
    public Complaint findComplaintByComplaintId(int complaintId) {
        return complaintMapper.findComplaintByComplaintId(complaintId);
    }

    @Override
    public List<Complaint> findComplaintByUserAccount(int userAccount) {
        return complaintMapper.findComplaintByUserAccount(userAccount);
    }

    @Override
    public List<Complaint> findComplaintByState(ComplaintsState complaintsState) {
        return complaintMapper.findComplaintByState(complaintsState);
    }

    @Override
    @Transactional
    public void updateComplaintByComplaintId(Complaint complaint, int complaintState, String adminId) {
        complaint.setAdminId(adminId);
        for (ComplaintsState complaintsState: ComplaintsState.values()){
            if (complaintsState.getCode() == complaintState){
                //此时已经找到了对应的枚举类实体
                Orders orders = new Orders();
                orders.setOrderId(complaint.getOrderId());
                //找到对应被投诉的订单
                orders = ordersMapper.findOrderByOrderId(orders);
                //找到被投诉的商品
                Goods byId = goodsMapper.findById(orders.getGoodsId());
                if (complaintsState == ComplaintsState.COMPLAINTS_SUCCESS){
                    //投诉成功  扣被投诉人信誉分10分
                    byId.getUser().setUserCredit(10);
                    userMapper.updateCreditByUserId(byId.getUser(), false);
                    //是否退款  退款
                    byId.getUser().setUserAccount(complaint.getUserAccount());
                    userMapper.updateMoneyByUserId(byId.getUser(), true);
                }else if(complaintsState == ComplaintsState.COMPLAINTS_BAD){
                    //恶意投诉  扣投诉人信誉分5分
                    byId.getUser().setUserCredit(5);
                    userMapper.updateCreditByUserId(byId.getUser(),false);
                }
                complaintMapper.updateComplaintByComplaintId(complaint);
            }
        }
    }

    @Override
    public void insertComplaint(Complaint complaint) {
        complaint.setComplaintTime(new Date());
        complaintMapper.insertComplaint(complaint);
    }
}
